package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonStudentSubjectiveDataBean;
import com.fancy.learncenter.bean.RealeaseDetailDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.fragment.CartoonExpandVideoFragment;
import com.fancy.learncenter.ui.fragment.CartoonPicSpeakFragment;
import com.fancy.learncenter.ui.fragment.CartoonSentenceSpeakFragment;
import com.fancy.learncenter.ui.fragment.CartoonSentenceSpeakLocalFragment;
import com.fancy.learncenter.ui.fragment.CartoonSongFragment;
import com.fancy.learncenter.ui.fragment.CartoonWordSpeakFragment;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonDetailActivity extends BaseActivity implements CartoonWordSpeakFragment.OnWorkSpeakListener {
    public static String LOCAL_FILE_PATH = "LOCAL_FILE_PATH";

    public static String QUESTION_ID = "QUESTION_ID";

    public static String isRealease = "";

    public static String DATA_KEY = "DATA_KEY";

    public static String ANSWER_KEY = "ANSWER_KEY";

    public static String RELEASE_KEY = "RELEASE_KEY";

    public static String answerStr = "";
    public static String type = "";
    Fragment fragment;

    String locaPath;

    String data;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    ArrayList<String> ansList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_cartoon_detail, Status.STATUS);

        String subjectQuestionId = getIntent().getStringExtra(QUESTION_ID);
        locaPath = getIntent().getStringExtra(LOCAL_FILE_PATH);
        type = getIntent().getStringExtra("type");


        answerStr = getIntent().getStringExtra(ANSWER_KEY);
        data = getIntent().getStringExtra(DATA_KEY);

        ansList = getIntent().getStringArrayListExtra(RELEASE_KEY);
        if (!TextUtils.isEmpty(subjectQuestionId)) {
            Map<String, String> map = Utils.getSudentKeyValueRes();


            Gson gson = new Gson();
            CartoonStudentSubjectiveDataBean cartoonStudentSubjectiveDataBean = gson.fromJson(map.get(subjectQuestionId), CartoonStudentSubjectiveDataBean.class);//把JSON字符串转为对象

//            if (cartoonStudentSubjectiveDataBean != null) {
//                getFgDetail(cartoonStudentSubjectiveDataBean);
//            } else {
//                getDatas(subjectQuestionId);
//            }


            getDatas(subjectQuestionId);
        }
    }


    private void getDatas(String speakPackageId) {

        Map map = new HashMap();
        map.put("subjectQuestionId", speakPackageId);
        HttpMehtod.getInstance().releaseDetail(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<RealeaseDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), RealeaseDetailDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<RealeaseDetailDataBean>() {
                    @Override
                    public void success(RealeaseDetailDataBean dataBean) {
                        getFgDetail(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {
                        LogUtil.MyLog("errorCode", "===errorCode===" + errorCode);
                    }
                }).dealData();

            }
        });
    }

    private void getFgDetail(CartoonStudentSubjectiveDataBean itemData) {
        switch (itemData.getCategoryId()) {
            case "7":
                fragment = CartoonWordSpeakFragment.newInstance(true, false, answerStr, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());

//                if (TextUtils.isEmpty(locaPath)) {
//
//                } else {
//
//                    fragment = CartoonWordSpeakFragment.newInstance(false, true, answerStr, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
//
//                }

//                                    fragments.add();
                break;
            case "8":
                if (TextUtils.isEmpty(locaPath)) {
                    fragment = CartoonSentenceSpeakLocalFragment.newInstance(answerStr, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
                } else {

                    if (TextUtils.isEmpty(type)) {
                        fragment = CartoonSentenceSpeakLocalFragment.newInstance(locaPath, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
                    } else {

//                        if ("3".equals(type)) {
//                            if (ansList != null) {
//                                fragment = CartoonSentenceSpeakLocalFragment.newInstance(locaPath, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList(), type, ansList);
//                            }
//
//                        } else {
//                            fragment = CartoonSentenceSpeakLocalFragment.newInstance(locaPath, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList(), type);
//
//                        }
                        fragment = CartoonSentenceSpeakLocalFragment.newInstance(locaPath, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList(), type);

                    }

                }
                break;
            case "9":
//                                    fragments.add(new CartoonPicSpeakFragment("123"));
                break;
//                                    fragments.add(new CartoonSongFragment(1, FileUtils.getEveryDayRecorderAudioPath()));
//            case "11":
////                                    fragments.add(new CartoonExpandVideoFragment());
//                break;

            case "10":
                if (TextUtils.isEmpty(locaPath)) {
                    fragment = CartoonSentenceSpeakLocalFragment.newInstance(answerStr, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
                } else {
                    fragment = CartoonSentenceSpeakLocalFragment.newInstance(locaPath, itemData.getSubtitleJsonStr(), (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
                }
//                fragments.add(CartoonSongFragment.newInstance(dataBean.getResult().getSpeakPackageId(), subjectQuestionVOListBean.getSubjectQuestionId() + "", subjectQuestionVOListBean.getQuestionName(), subjectQuestionVOListBean.getSubtitleJsonStr(), packagePath, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                break;
            case "11":
                fragment = CartoonExpandVideoFragment.newInstance((ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) itemData.getStemsList());
                break;
//                fragments.add(CartoonExpandVideoFragment.newInstance((ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
        }
        //给ViewPager设置适配器
        // mFragmentAdapteradapter.notifyDataSetChanged();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment).show(fragment).commit();
    }

    @Override
    public void onWorkSpeakListener(boolean isDO, int totalNum, String title) {

    }
}
